
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.GenericsUtils;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Condition;
import com.jf.cloud.service.sysmanager.dao.IPubgBaseAfficheService;
import com.jf.cloud.service.sysmanager.entity.PubgBaseAffiche;

/**
 * 公告服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgBaseAfficheService")
public class PubgBaseAfficheServiceImpl extends BaseService<PubgBaseAffiche, Long> implements IPubgBaseAfficheService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgBaseAfficheServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	@SuppressWarnings("unchecked")
	private RowMapper<PubgBaseAffiche> rowMapper = new BeanPropertyRowMapper<PubgBaseAffiche>(GenericsUtils.getSuperClassGenricType(this.getClass()));

	/**
	 * 根据主键查询公告
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public PubgBaseAffiche findPubgBaseAffiche(Long rowId) throws Exception {
		try {
			return this.baseDao.findObject("", rowMapper, rowId);
		} catch (Exception e) {
			throw new Exception("根据主键查询公告出错！", e);
		}
	}

	/**
	 * 根据查询条件获取公告列表
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public List<PubgBaseAffiche> findPubgBaseAfficheList(Condition qc) throws Exception {
		try {
			return this.baseDao.findList("", rowMapper);
		} catch (Exception e) {
			throw new Exception("根据查询条件获取公告列表出错！", e);
		}
	}

	/**
	 * 新增/修改公告
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public PubgBaseAffiche saveOrUpdatePubgBaseAffiche(PubgBaseAffiche pubgBaseAffiche) throws Exception {
		try {
			return this.baseDao.saveAll(pubgBaseAffiche);
		} catch (Exception e) {
			throw new Exception("新增/修改公告出错！", e);
		}
	}

	/**
	 * 更新公告
	 *
	 * @author 代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public void updatePubgBaseAffiche(PubgBaseAffiche pubgBaseAffiche) throws Exception {
		try {
			StringBuilder updateSql = new StringBuilder("UPDATE pubg_base_affiche SET OPT_ID = ?, OPT_ACCOUNT = ?, OPT_TIME = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(pubgBaseAffiche.getOptId());
			params.add(pubgBaseAffiche.getOptAccount());
			params.add(pubgBaseAffiche.getOptTime());
			if (pubgBaseAffiche.getOrderByNo() != null) {
				updateSql.append(", ORDER_BY_NO = ?");
				params.add(pubgBaseAffiche.getOrderByNo());
			}
			if (StringUtil.isNotEmpty(pubgBaseAffiche.getNoticeTitle())) {
				updateSql.append(", NOTICE_TITLE = ?");
				params.add(pubgBaseAffiche.getNoticeTitle());
			}
			if (pubgBaseAffiche.getFbFlag() != null) {
				updateSql.append(", FB_FLAG = ?");
				params.add(pubgBaseAffiche.getFbFlag());
			}
			if (StringUtil.isNotEmpty(pubgBaseAffiche.getImgPath())) {
				updateSql.append(", IMG_PATH = ?");
				params.add(pubgBaseAffiche.getImgPath());
			}
			if (StringUtil.isNotEmpty(pubgBaseAffiche.getNoticeContent())) {
				updateSql.append(", NOTICE_CONTENT = ?");
				params.add(pubgBaseAffiche.getNoticeContent());
			}
			if (pubgBaseAffiche.getDelFlag() != null) {
				updateSql.append(", DEL_FLAG = ?");
				params.add(pubgBaseAffiche.getDelFlag());
			}
			updateSql.append(" WHERE ROW_ID=? ");
			params.add(pubgBaseAffiche.getRowId());
			baseDao.update(updateSql.toString(), params.toArray());
		} catch (Exception e) {
			throw new RuntimeException("更新公告出错！", e);
		}
	}
}
